﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Bank_database_system.业务部
{
    public partial class 定期存款a : UserControl
    {
        int pagemax = 0;
        int pages = 0;
        public event EventHandler ButtonBack;
        public 定期存款a()
        {
            InitializeComponent();
        }

        public void CardID_Set(string cardID)
        {
            textBox1.Text = cardID;
            textBox5.Text = AccountCheck.Balance_Inquiry(cardID).ToString();
        }

        public void get()
        {
            pagemax = TimeDeposit.count();
            if (pagemax != 0)
            {
                pagemax = (pagemax-1) / 10 + 1;
                dataGridView1.DataSource = TimeDeposit.QueryDepositType(1);
                pages = 1;
                dataGridView1.RowHeadersWidth = 50;
                dataGridView1.Columns["TIME_DEPOSIT_TYPE_ID"].Width = 180;
                dataGridView1.Columns["TIME_DEPOSIT_TYPE_ID"].HeaderText = "定期存款类型ID";
                dataGridView1.Columns["TIME_DEPOSIT_TIME"].Width = 140;
                dataGridView1.Columns["TIME_DEPOSIT_TIME"].HeaderText = "定期时间";
                dataGridView1.Columns["INTEREST_RATE"].Width = 150;
                dataGridView1.Columns["INTEREST_RATE"].HeaderText = "定期存款利率";
                dataGridView1.Columns["QUOTA"].Width = 150;
                dataGridView1.Columns["QUOTA"].HeaderText = "定期存款限额";
                dataGridView1.Columns["RNUM"].Visible = false;
            }
            else
            {
                pages = 0;
            }
            label12.Text = pages.ToString();
            label7.Text = pagemax.ToString();
        }
        private void button8_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            textBox5.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            dataGridView1.DataSource = null;
            pages = 0;
            pagemax = 0;
            ButtonBack?.Invoke(this, EventArgs.Empty);
        }

        private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
        {
            //对输入内容做限制
            bool flag = true;
            if (textBox3.Text != "")
            {
                if (textBox3.Text.Contains('.'))
                {
                    flag = false;
                }
                else
                {
                    flag = true;
                }
            }

            if (flag == false && (e.KeyChar > '9' || e.KeyChar < '0') && e.KeyChar != '\b')
            {
                e.Handled = true; // 不为空且有小数点时时拒绝输入非数字和退格的字符
            }
            else if (((e.KeyChar > '9' || e.KeyChar < '0') && (e.KeyChar != '.' && e.KeyChar != '\b')))
            {
                e.Handled = true; // 无小数点时时拒绝输入非数字、小数点、退格的字符
            }

        }
        private void textBox3_leave(object sender, EventArgs e)
        {
            //整理内容,消除首位0，清除末尾.和.后末尾0，给.开头添加0
            if (textBox3.Text != "")
            {
                if (textBox3.Text[0] == '0')
                {
                    textBox3.Text = textBox3.Text.TrimStart('0');
                    if (textBox3.Text != "" && textBox3.Text[0] == '.')
                        textBox3.Text = "0" + textBox3.Text;
                }
                else if (textBox3.Text[0] == '.')
                {
                    textBox3.Text = "0" + textBox3.Text;
                }
                if (textBox3.Text.Contains('.'))
                {
                    textBox3.Text = textBox3.Text.TrimEnd('0');
                    if (textBox3.Text.EndsWith("."))
                        textBox3.Text = textBox3.Text.TrimEnd('.');
                }
            }
        }

        private void button9_Click(object sender, EventArgs e)
        {
            if (textBox3.Text != "")//需要加选择条件！！！
            {
                DialogResult result = MessageBox.Show("请确认是否以" + textBox2.Text + " 的方式从账户 " + textBox1.Text + " 定期存入金额 " + textBox3.Text + " (CNY)。", "确认保存", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (result == DialogResult.Yes)
                {
                    //密码验证
                    //定期存款过程

                    string describe = "";
                    decimal amount = 0;
                    if (!decimal.TryParse(textBox3.Text, out amount))
                    {
                        describe = "定期存款金额有误";
                        goto error;
                    }
                    string hostName = Dns.GetHostName(); // 获取主机名
                    IPAddress[] ipAddresses = Dns.GetHostAddresses(hostName); // 获取本机的IP地址

                    // 过滤出IPv4地址
                    IPAddress ipv4 = ipAddresses.FirstOrDefault(ip => ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork);
                    if (ipv4 == null)
                    {
                        describe = "获取IP地址出现错误";
                        goto error;
                    }
                    string ip = ipv4.ToString();
                    using (输入密码 p = new 输入密码(textBox1.Text))
                    {
                        DialogResult q = p.ShowDialog();
                        if (q == DialogResult.OK)
                        {
                            describe = TimeDeposit.ApplyDeposit(textBox1.Text, textBox2.Text, amount, ip, AccountCheck.staffid);
                        }
                        else if (q == DialogResult.No)
                        {
                            describe = "密码错误";
                        }
                    }
                error:
                    MessageBox.Show(describe);
                    textBox2.Text = ""; textBox3.Text = "";
                    CardID_Set(textBox1.Text);
                }
            }
            else if (textBox2.Text == "")
            {
                MessageBox.Show("请选择存款类型！");
            }
            else if (textBox3.Text == "")
            {
                MessageBox.Show("请输入存款金额！");
            }
        }

        private void button12_Click(object sender, EventArgs e)
        {
            if (pages != 0)
            {
                pages--;
                if (pages == 0)
                {
                    pages = 1;
                }
                else
                {
                    dataGridView1.DataSource = TimeDeposit.QueryDepositType(pages);
                }
            }
            label12.Text = pages.ToString();
        }

        private void button13_Click(object sender, EventArgs e)
        {
            if (pages != 0)
            {
                pages++;
                if (pages > pagemax)
                {
                    pages = pagemax;
                }
                else
                {
                    dataGridView1.DataSource = TimeDeposit.QueryDepositType(pages);
                }
            }
            label12.Text = pages.ToString();
        }

        private void dataGridView1_SelectionRow(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count == 1)
            {
                textBox2.Text = dataGridView1.SelectedRows[0].Cells["TIME_DEPOSIT_TYPE_ID"].Value.ToString();
            }
            else
            {
                textBox2.Text = "";
            }
        }

    }
}
